from PIL import Image
import sys
import os
import shutil


'''
用法：
目标目录结构   第一层目录->第二层SKU一级目录->第三层SKU二级目录
'''

def pad_image(image,tarsize=(0,0)):

    """
    :param image: input image, tarsize: target size tuple default=(0,0))
    :return: new image
    """
    new_image = None

    if tarsize==(0,0):
        iw, ih = image.size  # 原始图像的尺寸
        nw, nh = (iw, ih)  # 目标图像的尺寸

        print("original size: ",(iw,ih))

        if iw/ih <= 0.75:
            nw = int(ih*0.75+0.5)
        else:
            nh = int(iw/0.75+0.5)

        print("now nums are: ", (nw, nh))

        #image = image.resize((nw, nh), Image.BICUBIC)  # 更改图像尺寸，双立法插值效果很好
        #image.show()
        new_image = Image.new('RGB', (nw, nh), (255, 255, 255))  # 生成白色背景
        # // 为整数除法，计算图像的位置
        new_image.paste(image, ((nw - iw) // 2, (nh - ih) // 2))  # 将图像填充为中间图像，两侧为白色的样式
        #new_image.show()
    else:
        iw, ih = image.size  # 原始图像的尺寸
        w, h = tarsize  # 目标图像的尺寸

        print("original size: ",(iw,ih))
        print("new size: ", (w, h))

        scale = min(w / iw, h / ih)  # 转换的最小比例

        # 保证长或宽，至少一个符合目标图像的尺寸 0.5保证四舍五入
        nw = int(iw * scale+0.5)
        nh = int(ih * scale+0.5)

        print("now nums are: ", (nw, nh))

        image = image.resize((nw, nh), Image.BICUBIC)  # 更改图像尺寸，双立法插值效果很好
        #image.show()
        new_image = Image.new('RGB', tarsize, (255, 255, 255))  # 生成白色背景
        # // 为整数除法，计算图像的位置
        new_image.paste(image, ((w - nw) // 2, (h - nh) // 2))  # 将图像填充为中间图像，两侧为黑色的样式
        #new_image.show()

    return new_image

def out3x4(srcdir):
    # 创建输出文件夹
    out_dir = srcdir+'-3x4'
    out_dir = os.path.join(srcdir, out_dir)
    if os.path.exists(out_dir):
        shutil.rmtree(out_dir)
    os.mkdir(out_dir)
    flist = os.listdir(srcdir)
    for f in flist:
        path = os.path.join(srcdir, f)
        file_ext = f.rsplit('.',maxsplit=1)

        if len(file_ext)!=2:
            continue
        if file_ext[1]!='jpg' and file_ext[1]!='jpeg':
            continue

        image = Image.open(path)
        newImage=pad_image(image, (0,0))
        newImage.save(os.path.join(out_dir, f'{file_ext[0]}-3x4.{file_ext[1]}'))

def out100x100(srcdir):
    # 创建输出文件夹
    out_dir = srcdir+'-100x100'
    out_dir = os.path.join(srcdir, out_dir)
    if os.path.exists(out_dir):
        shutil.rmtree(out_dir)
    os.mkdir(out_dir)
    flist = os.listdir(srcdir)
    for f in flist:
        path = os.path.join(srcdir, f)
        file_ext = f.rsplit('.',maxsplit=1)

        if len(file_ext)!=2:
            continue
        if file_ext[1]!='jpg' and file_ext[1]!='jpeg':
            continue

        image = Image.open(path)
        size = (100, 100)
        newImage=pad_image(image, size)
        newImage.save(os.path.join(out_dir, f'{file_ext[0]}-100x100.{file_ext[1]}'))

def main():
    if len(sys.argv)!=2:
        print("参数错误，请输入文件夹名称\n")
        exit()
    ori_dir = sys.argv[1]
    if not os.path.isdir(ori_dir):
        print("目标参数不是文件夹\n")
        exit()

    flist = os.listdir(ori_dir)

    for f in flist:
        subpath = os.path.join(ori_dir, f)
        if os.path.isdir(subpath):
            subflist = os.listdir(subpath)
            for subf in subflist:
                cf = os.path.basename(subpath)
                trdpath = os.path.join(subpath, subf)
                if os.path.isdir(trdpath) and subf==cf:
                    print(trdpath)
                    out3x4(trdpath)
                    out100x100(trdpath)


if __name__ == '__main__':
    main()